/*

*/

client
	verb
		show_book()
			set instant=1
			if(!mob.BOOK)
				mob << "There is no book to be seen!"
				return
			update_book()
			winshow(src,"book")

		show_newfoo(type as text)
			set instant=1
			if(findtext(type,"x"))type += "es"
			else type += "s"
			show_newfoo_args(type)

		add_new(type as text)
			set instant=1

			var/found

			var/name = winget(src, "newfoo.name","text")
			var/desc = winget(src, "newfoo.desc","text")

			if(mob.vars["known_[lowertext(type)]"][name]) found=1

			mob.vars["known_[lowertext(type)]"][name] = desc

			winshow(src, "newfoo", 0)
			src << "I have [found ? "modified" : "added"] \"[name]\" [found ? "in" : "to"] my [type] glossary."
			update_toc()
			update_component(type)

			winset(src, "newfoo.name","text=\"\"")
			winset(src, "newfoo.desc","text=\"\"")

	proc
		show_newfoo_args(type, name, desc)
			if(winget(src,"newfoo","is-visible")=="true") return

			winset(src, "newfoo.typename","text=\"[name]\"")
			winset(src, "newfoo.typedesc","text=\"[desc]\"")

			winset(src, "newfoo","title=\"Create new [type]\"")
			winset(src, "newfoo.confirm","command=\"add-new [type]\"")
			winset(src, "newfoo.typename","text=\"Name of [type]:\"")
			winset(src, "newfoo.typedesc","text=\"Description of [type]:\"")
			winshow(src, "newfoo")

		update_book()
			update_toc()
			update_spells()
			update_component("prefixes")
			update_component("roots")
			update_component("suffixes")

		update_toc()
			var/book/b = mob.BOOK
			var/player/p = mob
			winset(src, "TOC.spell_count","text=\"Total Spells in Book: [count_spells()]/[b.spells.len] ([round(100*(count_spells()/b.spells.len))]%)\"")
			winset(src, "TOC.prefixes","text=\"\
			Total Verified Prefixes:      [count_verified("prefixes")]\n			\
			Total Unverified Prefixes:    [count_unverified("prefixes")]\n			\
			Percent Prefix Verified:      [(p.known_prefixes.len>0 ? round((count_verified("prefixes")/p.known_prefixes.len)*100) : 0)]%\n			\
			\"")
			winset(src, "TOC.roots","text=\"\
			Total Verified Roots:         [count_verified("roots")]\n			\
			Total Unverified Roots:       [count_unverified("roots")]\n			\
			Percent Root Verified:        [(p.known_roots.len>0 ? round((count_verified("roots")/p.known_roots.len)*100) : 0)]%\n			\
			\"")
			winset(src, "TOC.suffixes","text=\"\
			Total Verified Suffixes:      [count_verified("suffixes")]\n			\
			Total Unverified Suffixes:    [count_unverified("suffixes")]\n			\
			Percent Suffix Verified:      [(p.known_suffixes.len>0 ? round((count_verified("suffixes")/p.known_suffixes.len)*100) : 0)]%\n			\
			\"")
			winset(src, "TOC.book_stats","text=\"\
			Total Pages in Book:        [b.capacity]\n\
			Total Characters Per Page:  [b.length]\n\
			\"")

		count_spells()
			var/book/b = mob.BOOK
			for(var/o in b.spells)
				if(isspell(o))
					.++
			if(!.).=0

		count_verified(type)
			var/list/l = list_verified(type)
			return l.len

		count_unverified(type)
			var/list/l = list_unverified(type)
			return l.len

		list_verified(type)
			var/player/p = mob
			. = list()
			for(var/o in mob.vars["known_[lowertext(type)]"])
				if(o in p.verified_names)
					.+=o

		list_unverified(type)
			var/player/p = mob
			. = list()
			for(var/o in mob.vars["known_[lowertext(type)]"])
				if(!(o in p.verified_names))
					.+=o

		update_spells()
			var/book/b = mob.BOOK
			for(var/x=1 to b.capacity+1)
				if(x<10)
					if(winexists(src,"child[x]"))winset(src,"child[x]","parent=null")

					var/list/child = new /list()
					child["type"] = "child"

					child["pos"] = "pos=0,[60*(x-1)]"

					child["is-visible"] = "true"

					child["parent"] = "Spells"

					child["size"] = "size=624x60"

					child["id"] = "child[x]"

					winset(src, "child[x]", list2params(child))

					winclone(src, "spell_pane", "spell_pane[x]")
					winset(src, "child[x]","left=spell_pane[x]")
				else
					winclone(src, "spell_pane", "spell_pane[x]")
					winset(src, "child[x-10]","is-vert=true")
					winset(src, "child[x-10]","show-splitter=false")
					winset(src, "child[x-10]","right=spell_pane[x]")
				if(b.spells.len >= x && b.spells[x])
					var/spell/s = b.spells[x]
					winset(src, "spell_pane[x].name","text=\"[s]\"")
					winset(src, "spell_pane[x].desc","is-visible=true")
					if(b.spells[s])
						winset(src, "spell_pane[x].desc","text=\"[b.spells[s]]\"")
					var/f = fcopy_rsc(icon(s.icon, s.icon_state))
					winset(src, "spell_pane[x].image","image=\"\ref[f]\"")
					winset(src, "spell_pane[x].remove","is-visible=true")
					winset(src, "spell_pane[x].desc","is-editable=true")

		update_component(type)
			var/this_grid
			switch(type)
				if("prefixes")this_grid = "GlossPre"
				if("suffixes")this_grid = "GlossSuf"
				if("roots"	 )this_grid = "GlossRoot"
			src << output(null, "[this_grid].v_output")

			var/list/known = list_verified(type)
			if(known.len)src << output("Verified [type]:","[this_grid].v_output")
			for(var/x in known)
				var/desc = mob.vars["known_[type]"][x]
				src << output("<a href=\"?modify&type=[type]&name=[x][desc?"&desc=[desc]":""]\">[x]</a> [desc ? " - [desc]" : ""]","[this_grid].v_output")

			if(known.len>0)src << output("","[this_grid].v_output")

			known = list_unverified(type)
			if(known.len)src << output("Unverified [type]:","[this_grid].v_output")
			for(var/x in known)
				var/desc = mob.vars["known_[type]"][x]
				src << output("<a href=\"?modify&type=[type]&name=[x][desc?"&desc=[desc]":""]\">[x]</a> [desc ? " - [desc]" : ""]","[this_grid].v_output")


